package org.algorithm.Sort;

public class BubbleSort {

    public static void main(String[] args) {
        int[] array = {3, 1, 4, 2};
        bubbleSort1(array);
        for (int num : array) {
            System.out.println(num);
        }
    }

    public static void bubbleSort3(int[] nums) {
        int n = nums.length;

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n - i - 1; j++) {
                if (nums[j] > nums[j + 1]) {
                    swap(nums, j, j + 1);
                }
            }
        }
    }

    public static void bubbleSort2(int[] nums) {
        int n = nums.length;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n - i - 1; j++) {
                if (nums[j] > nums[j + 1]) {
                    swap(nums, j, j + 1);
                }
            }
        }
    }

    public static void bubbleSort1(int [] nums){
        int n = nums.length;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n-i-1; j++) {
                if (nums[j]>nums[j+1]){
                    swap(nums,j,j+1);
                }
            }
        }
    }


    public static void bubbleSort(int[] nums) {
        int n = nums.length;
        for (int i = 0; i < n - 1; i++) {
            for (int j = 0; j < n - i - 1; j++) {
                if (nums[j] > nums[j + 1]) {
                    swap(nums, j, j + 1);
                }
            }
        }
    }

    public static void customSort(int[] array) {
        int n = array.length;
        for (int i = 0; i < n; i++) {
            // 使用一个临时变量保存当前比较的元素
            int current = array[i];
            for (int j = i + 1; j < n; j++) {
                if (current > array[j]) {
                    // 交换 array[i] 和 array[j]
                    array[i] = array[j];
                    array[j] = current;
                    current = array[i];
                }
            }
        }
    }

    public static void swap(int[] array, int index1, int index2) {
        int temp = array[index1];
        array[index1] = array[index2];
        array[index2] = temp;
    }


}


